package a08_回溯算法;

import java.util.*;

/**
 * <p>
 * a14_递增子序列复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/2/24
 */
public class a14_递增子序列复习2 {

    public static void main(String[] args) {
        a14_递增子序列复习2 test = new a14_递增子序列复习2();
        System.out.println(test.findSubsequences(new int[]{4, 6, 7, 7}));
    }

    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();
        traversal(result, path, nums,0);
        return result;
    }

    private void traversal(List<List<Integer>> result,
                           LinkedList<Integer> path,
                           int[] nums,
                           int startIdx){

        if(path.size() > 1){
            result.add(new ArrayList<>(path));
        }

        Set<Integer> set = new HashSet<>();
        for (int i = startIdx; i < nums.length; i++) {
            if(path.size() > 0 && path.peekLast() > nums[i]){
                continue;
            }else {
                if(set.contains(nums[i])){
                    continue;
                }
                set.add(nums[i]);
                path.add(nums[i]);
                traversal(result, path, nums,i+1);
                path.removeLast();
            }
        }
    }
}
